package com.david.course_design.shopping_center.module.goods.goods.controller;

import com.david.course_design.shopping_center.module.goods.goods.entity.Goods;
import com.david.course_design.shopping_center.module.goods.goods.service.GoodsService;
import com.david.course_design.shopping_center.result.PageObject;
import com.david.course_design.shopping_center.result.ResultBody;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@Api(tags = "商品接口")
@RequestMapping("Goods/")
public class GoodsController {
    //  依赖注入 dependency injection
    @Resource
    private GoodsService goodsService;

    @GetMapping("selectGoodsById")
    @ApiOperation("通过商品主键（id）查询商品信息")
    private ResultBody selectGoodsById(Integer id) {
        Goods goods = goodsService.selectGoodsById(id);
        if (goods != null) {
            return ResultBody.success(goods);
        } else {
            return ResultBody.error("", "没有找到数据!");
        }
    }

    @GetMapping("selectGoodsPaging")
    @ApiOperation("查询所有商品信息（分页）")
    private ResultBody selectGoodsPaging(PageObject pageObject) {
        PageInfo<Goods> goodsPageInfo = goodsService.selectGoodsPaging(pageObject);

        if (goodsPageInfo != null) {
            return ResultBody.success(goodsPageInfo);
        } else {
            return ResultBody.error("", "没有找到数据!");
        }
    }

    @GetMapping("selectAllGoods")
    @ApiOperation("查询所有商品信息（不分页）")
    private ResultBody selectAllGoods() {
        List<Goods> goods = goodsService.selectAllGoods();
        if (goods != null) {
            return ResultBody.success(goods);
        } else {
            return ResultBody.error("", "没有找到数据!");
        }
    }
}
